package game;

import java.util.ArrayList;

public class Battle 
{
	ArrayList<Character> nasi;
	ArrayList<Character> njihovi;
	boolean isTurnActive;
	Character naPotezu = null;
	boolean borbaTraje = true;
	boolean intelligence1 = false,intelligence2 = false;
	AI ai;
	
	public boolean isTurnActive() {
		return isTurnActive;
	}
	
	public void setFriendlyIntelligence(boolean b)
	{
		intelligence2 = b;
	}
	
	public void setEnemyIntelligence(boolean b)
	{
		intelligence1 = b;
	}
	
	public ArrayList<Character> getEnemies()
	{
		return njihovi;
	}
	
	public ArrayList<Character> getFriendlies()
	{
		return nasi;
	}

	public Battle(ArrayList<Character> n, ArrayList<Character> nj)
	{
		this.nasi = n;
		this.njihovi = nj;
		naPotezu = koIgra();
		ai = new AI(this);
	}
	
	
	private Character koIgra(){
		int max1 = -1;
		Character maxC1 = null;
		int max2 = -1;
		Character maxC2 = null;
		for(Character c: nasi){
			if (c.getActionPoints() > max1){
				max1 = c.getSpeed();
				maxC1 = c;
			}
		}
		for(Character c: njihovi){
			if (c.getActionPoints() > max2){
				max2 = c.getSpeed();
				maxC2 = c;
			}
		}
		if (maxC1.getActionPoints() > maxC2.getActionPoints()) return maxC1;
		else return maxC2;
	}
	
	public boolean areNasiZivi()
	{
		int t1 = 0;
		for(Character c : nasi)
			if(c.isDead()) t1++;
		return t1 < nasi.size();
	}
	
	public boolean areNjihoviZivi()
	{
		int t2 = 0;
		for(Character c : njihovi)
			if(c.isDead()) t2++;
		return t2 < njihovi.size();
	}
	
	public boolean doesBorbaTraje()
	{
		return borbaTraje;
	}
	
	public void prebaciPotez(){
		if(!borbaTraje) return;
		
		for(Character c : nasi)
			if(!c.isDead()) c.BattleTurnPassed();
		for(Character c : njihovi)
			if(!c.isDead()) c.BattleTurnPassed();

		if(!areNasiZivi() || !areNjihoviZivi())
		{
			borbaTraje = false;
			System.out.println("BATTLE : Gotovo, neki su pomrli");
			return;
		}
		
		isTurnActive = true;
		naPotezu = koIgra();
		
		/*
		System.out.println("Tim 1 health :");
		for(Character c : nasi)
			System.out.println("  * " + c.getName() + " - " + c.getHealth());
		System.out.println("Tim 2 health :");
		for(Character c : njihovi)
			System.out.println("  * " + c.getName() + " - " + c.getHealth());
		*/
		
		naPotezu.BattleTurnActivated();
		
//		if(nasi.contains(naPotezu) && intelligence2) ai.playTurn();
		
		System.out.println("\nBATTLE : Na potezu je " + naPotezu.getNameHP());
		if(njihovi.contains(naPotezu) && intelligence1) ai.playTurn();
	}
	
	public Character getNaPotezu()
	{
		return naPotezu;
	}
	
	public boolean areOnSameTeam(Character c1, Character c2)
	{
		return ((nasi.contains(c1) && nasi.contains(c2)) ||
		(njihovi.contains(c1) && njihovi.contains(c2)));
	}
	
	public boolean baciSpell(Spell spell, Character target){
		System.out.println("BATTLE : " + naPotezu.getNameHP() + " baca spell");
		if(!isTurnActive) { System.out.println("Potez već iskorišten!"); return false; }
		Character attacker = koIgra();
		
		if(spell.isTargeted())
		{
			if(spell.isDefensive())
			{
				if(!areOnSameTeam(naPotezu, target))
				{
					System.out.println("BATTLE : Defanzivan spell bacen na protivnika, no can do");
					return false;
				}
			}
			else
			{
				if(areOnSameTeam(naPotezu, target))
				{
					System.out.println("BATTLE : Friendly fire, no can do");
					return false;
				}
			}
		}
		else
		{
			if(target != null)
			{
				System.out.println("BATTLE : Netargetovan spell ima target?");
			}
		}
		
		if (attacker.CastSpell(spell, target)){
			//odradiPotez(target);
			isTurnActive = false;
			return true;
		}
		else return false;
	}
	public boolean napadni(Character target){
		System.out.println("BATTLE : " + naPotezu.getNameHP() + " napada " + target.getNameHP());
		if(!isTurnActive) { System.out.println("Potez već iskorišten!"); return false; }
		naPotezu.Attack(target);
		isTurnActive = false;
		//odradiPotez(target);
		return true;
	}
	public boolean koristiItem(Item item, Character target){
		System.out.println("BATTLE : " + naPotezu.getNameHP() + " koristi item " + item.getName());
		if(!isTurnActive) { System.out.println("Potez već iskorišten!"); return false; }
		if (naPotezu.UseItem(item)){
			//odradiPotez(target);
			isTurnActive = false;
			return true;
		}
		else return false;
	}
	

}
